package com.supreme.cacheserver.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.supreme.cacheserver.service.AccountService;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.commonmybatis.entity.primary.MBAccount;
import com.supreme.commonmybatis.mapper.AccountMapper;
import com.supreme.commonmybatis.service.quick.ExtAccountService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "account")
public class AccountController {

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private AccountService accountService;

    @Resource
    private ExtAccountService extAccountService;

    @PostMapping(value = "insert")
    public AjaxResp insertAccount(@RequestBody MBAccount account) {
        int result = accountMapper.insert(account);
        return AjaxResp.instance(result);
    }

    @PutMapping(value = "update/{id}")
    public AjaxResp updateAccount(@PathVariable(value = "id") Long id, @RequestBody MBAccount account) {
        account.setId(id);
        int result = accountMapper.updateById(account);
        return AjaxResp.instance(result);
    }

    @PutMapping(value = "updateMoney/{id}")
    public AjaxResp updateMoney(@PathVariable(value = "id") Long id, @RequestBody MBAccount account) {
        int result = accountMapper.update(null, Wrappers.lambdaUpdate(
                MBAccount.class).eq(MBAccount::getId, id).set(MBAccount::getMoney, account.getMoney())
        );
        return AjaxResp.instance(result);
    }

    @DeleteMapping(value = "delete/{id}")
    public AjaxResp deleteAccount(@PathVariable(value = "id") Long id) {
        int result = accountMapper.deleteById(id);
        return AjaxResp.instance(result);
    }

    @GetMapping(value = "select")
    public AjaxResp selectAccount() {
        List<MBAccount> accountList = accountMapper.selectList(Wrappers.emptyWrapper());
        return AjaxResp.instance(accountList);
    }

    @PostMapping(value = "insertOrUpdate")
    public AjaxResp insertOrUpdate(@RequestBody MBAccount account) {
        boolean saveOrUpdate = extAccountService.saveOrUpdate(account);
        return AjaxResp.instance(saveOrUpdate);
    }

    @PostMapping(value = "insertAccountListToRedis/{redisKey}")
    public AjaxResp insertAccountListToRedis(@PathVariable(value = "redisKey") String key, @RequestBody List<MBAccount> dataList) {
        accountService.insertAccountListToRedis(key, dataList);
        return AjaxResp.instance();
    }

    @GetMapping(value = "getAccountListFromRedis/{redisKey}")
    public AjaxResp getAccountListFromRedis(@PathVariable(value = "redisKey") String key) {
        List<MBAccount> testList = accountService.getAccountListFromRedis(key);
        return AjaxResp.instance(testList);
    }

    public AjaxResp getAccountByIndex() {
        return AjaxResp.instance();
    }
}
